package com.wintop.youyuan.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.Message;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.Photo;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.UserInformationServices;
import com.wintop.youyuan.services.UserServices;
import com.wintop.youyuan.util.ActionUtil;
import com.wintop.youyuan.util.CalculateUtil;

@Scope("prototype")
@Controller
public class SearchLoveAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private final int pageSize = 9;// 页数大小
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private UserServices userServices;
	private UserInformationServices userInformationServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private Message message;
	private Photo photo;
	private HashMap<String, Object> otherData;// 临时数据储存

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String redirectSearch() {// 搜缘首页
		user = (User) session.get("userInfo");
		if (user == null) {// 判断用户是否登录
			return "error";
		}
		int sex = 1 - user.getSex();
		if (otherData == null) {
			otherData = new HashMap<String, Object>();
			otherData.put("province", "安徽省");
			otherData.put("city", "全省");
			otherData.put("minAge", "18岁");
			otherData.put("maxAge", "不要求");
			otherData.put("pageNum", "1");
		}
		otherData.put("sex", ActionUtil.getSex(sex));

		String province = (String) otherData.get("province");
		String city = (String) otherData.get("city");
		if (city.equals("全省")) {
			city = null;
		}
		int minAge = CalculateUtil.getLoveAge((String) otherData.get("minAge"));
		int maxAge = CalculateUtil.getLoveAge((String) otherData.get("maxAge"));
		int pageNum = Integer.parseInt((String) otherData.get("pageNum")) - 1;

		int peopleNum = userInformationServices.getLoveNum(sex, province, city,
				minAge, maxAge);
		otherData.put("peopleNum", peopleNum);
		otherData.put("pages", (int) Math.ceil(peopleNum / pageSize) + 1);

		List<BaseData> loveBaseDatas = userInformationServices.getLoveUsers(
				sex, province, city, minAge, maxAge, pageNum * pageSize,
				pageSize);
		List<Object> objects = new ArrayList<Object>();
		for (BaseData bd : loveBaseDatas) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			User u = userServices.getUser(bd.getUserid());
			map.put("userid", u.getUserid());
			map.put("name", u.getName());
			map.put("id", u.getId());
			map.put("age", CalculateUtil.getAge(bd.getBirthday()));
			map.put("apartment", ActionUtil.getApartment(bd.getApartment()));
			map.put("education", ActionUtil.getEducation(bd.getEducation()));
			map.put("monthlyIncome",
					ActionUtil.getMonthlyIncome(bd.getMonthlyIncome()));
			map.put("icon", ActionUtil.getIcon(u.getUserid(), bd.getIcon()));
			// System.out.println(bd);
			objects.add(map);
		}
		otherData.put("loveInfo", objects);
		return "redirectSearch";
	}

	public String redirectCrowd() {// 人群专区
		redirectSearch();
		return "redirectCrowd";
	}

	public String redirectConstellation() {// 星座速配
		redirectSearch();
		return "redirectConstellation";
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	@Resource(name = "userServices")
	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	@Resource(name = "userInformationServices")
	public void setUserInformationServices(
			UserInformationServices userInformationServices) {
		this.userInformationServices = userInformationServices;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
